package kr.saeildamil.androidstudy.ch08;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

public class FreeLine extends Activity{
	MyView mv;
	ArrayList<VertexEntitiy> arList;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mv = new MyView(this);
		setContentView(mv);
		arList = new ArrayList<VertexEntitiy>();
	}
	
	public class MyView extends View{
		Paint mpaint;
		public MyView(Context context) {
			super(context);
			mpaint = new Paint();
			mpaint.setColor(Color.BLACK);
			mpaint.setStrokeWidth(3);
			mpaint.setAntiAlias(true);
		}
		
		@Override
		protected void onDraw(Canvas canvas) {
			super.onDraw(canvas);
			canvas.drawColor(Color.LTGRAY);
			for(int i=0;i<arList.size();i++){
				if(arList.get(i).isDraw()){
					canvas.drawLine(arList.get(i-1).getX(), arList.get(i-1).getY(), arList.get(i).getX(), arList.get(i).getY(), mpaint);
				}
			}
		}
		
		@Override
		public boolean onTouchEvent(MotionEvent event) {
			if(event.getAction() == MotionEvent.ACTION_DOWN){
				Toast.makeText(FreeLine.this, "ACTION_DOWN (" + event.getX() + "," + event.getY() + ")", 1000).show();
				arList.add(new VertexEntitiy(event.getX(), event.getY(), false));
				return true;
			}
			if(event.getAction() == MotionEvent.ACTION_MOVE){
				Toast.makeText(FreeLine.this, "ACTION_MOVE (" + event.getX() + "," + event.getY() + ")", 1000).show();
				arList.add(new VertexEntitiy(event.getX(), event.getY(), true));
				invalidate();
				return true;
			}
			return false;
		}
	}
	
}
